package com.northming.daytodo.util;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.northming.daytodo.model.UserInfo;


@Component
public class UserUtil {
	
	
	@Value("${daytodo.user.service.path}")
	private String userServicePath;
	
	private  UserInfo userInfo;
	
	
	private  Integer userId;
	
	
	
	
	public UserInfo getUserInfo() {
		return userInfo;
	}




	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}




	public Integer getUserId() {
		return userId;
	}




	public void setUserId(Integer userId) {
		this.userId = userId;
	}




	public  boolean validateSession(HttpServletRequest request){
		boolean canPass = false;
		String sessionToken = request.getParameter("sessionToken");
		RestTemplate restTemplate = new RestTemplate();
		if(sessionToken != null){
			userInfo = restTemplate.getForObject(userServicePath+"/user/getUserInfo?sessionToken="+sessionToken, UserInfo.class);
			if(userInfo!=null){
				userId = Integer.valueOf(userInfo.getId());
				canPass = true;
			}			
		}
		return canPass;
	}
}
